package jmine.tec.web.front.services.incluir;

import jmine.tec.database.pack.PackException;
import jmine.tec.database.pack.services.PackEntityPersistenceHelper;
import jmine.tec.di.annotation.Injected;
import jmine.tec.services.annotations.Execution;
import jmine.tec.services.annotations.Input;
import jmine.tec.services.api.export.ReferenceMap;
import jmine.tec.services.api.export.ServiceFiller;
import jmine.tec.services.export.impl.ExportUtils;
import jmine.tec.services.io.ServiceBean;
import jmine.tec.web.front.domain.BackOfficeRequestProperty;

/**
 * Abstract service for {@link BackOfficeRequestProperty}
 * 
 * @param <T> the type of the BackOfficeRequestProperty
 * @author vinicius 
 */
public abstract class AbstractBackOfficeRequestPropertyService<T extends BackOfficeRequestProperty> implements ServiceFiller<T> {

    public static final String MNEMONICO = "Mnemônico";

    public static final String DESCRIPTION = "Description";

    public static final String DATABASE_ID = "Database ID";

    private Long databaseId;

    private String mnemonic;

    private String description;

    private PackEntityPersistenceHelper helper;

    /**
     * Executes the service
     * 
     * @throws PackException PackException
     */
    @Execution
    public void execute() throws PackException {
        T property = this.helper.findOrCreate(getPropertyClass(), this.databaseId, this.mnemonic);
        property.setMnemonic(this.mnemonic);
        property.setDescription(this.description);
        fillSpecific(property);
        this.helper.save(property, this.databaseId);
    }

    /**
     * Fills the especific values of the concrete service
     * 
     * @param property property
     */
    protected void fillSpecific(T property) {
        // OK
    }

    /**
     * {@inheritDoc}
     */
    public void fillServiceBean(ServiceBean bean, ReferenceMap referenceMap, T entity) {
        ExportUtils.setService(bean, this.getClass());
        ExportUtils.putDatabaseId(bean, DATABASE_ID, entity);
        ExportUtils.put(bean, MNEMONICO, entity.getMnemonic());
        ExportUtils.put(bean, DESCRIPTION, entity.getDescription());
    }

    /**
     * @return the property class
     */
    protected abstract Class<T> getPropertyClass();

    /**
     * @param helper the helper to set
     */
    @Injected
    public void setHelper(PackEntityPersistenceHelper helper) {
        this.helper = helper;
    }

    /**
     * @param databaseId the databaseId to set
     */
    @Input(fieldName = DATABASE_ID, required = true)
    public void setDatabaseId(Long databaseId) {
        this.databaseId = databaseId;
    }

    /**
     * @param mnemonic the mnemonic to set
     */
    @Input(fieldName = MNEMONICO, required = true)
    public void setMnemonic(String mnemonic) {
        this.mnemonic = mnemonic;
    }

    /**
     * @param description the description to set
     */
    @Input(fieldName = DESCRIPTION, required = true)
    public void setDescription(String description) {
        this.description = description;
    }
}
